# -*- coding: utf-8 -*-

maxsize = 20 # 哈希表最大存放20个哈希值
HashTable = {i:None for i in range(maxsize)} # 建立值为空的哈希表（字典）
print('哈希表大小为%d' % len(HashTable))

# 哈希函数，采用除留余数法求哈希address
def Hash(value):
    address = value % maxsize
    if HashTable[address] == None:
        return address
    else: # 考虑键值冲突，试探下一个空值哈希表位置
        while address < maxsize:
            address += 1 # 采用地址加1的方法试探下一个新的地址
            if HashTable[address] == None:
                return address
    return -1

def BuildHashTable(Values):
    for value in Values:
        address = Hash(value)
        print('建立的键值对(%d %d)' %(address, value))
        if address != -1:
            HashTable[address] = value
        else:
            print('数值%d存放的哈希表地址不够！' %(value))
    return HashTable

def FindValue(HashT, value): # 通过哈希地址找哈系表的值
    address = value % maxsize
    if HashT[address] == value:
        return True
    else:
        address += 1
        while address < maxsize:
            if HashT[address] == value:
                return True
            address += 1
    return False

s1 = [20,30,1,2,5,8,23,44,48]
value = 44
if FindValue(BuildHashTable(s1), value):
    print('在哈希表里找到%d' % (value))
else:
    print('在哈希表里找不到%d' % (value))

